home *** CD-ROM | disk | FTP | other *** search
- #include "DemoRoutines.h"
-
- /* Routine to take a 1-bit offscreen and map it to a 4-bit offscreen 4x smaller */
-
- void Scale1BitTo4Bit( GWorldPtr src, GWorldPtr dst )
- {
- PixMapHandle srcPixMap;
- PixMapHandle dstPixMap;
- short srcRowBytes;
- short dstRowBytes;
- long *srcBaseAddr;
- long *srcAddr1;
- long *srcAddr2;
- long *srcAddr3;
- long *srcAddr4;
- long *dstBaseAddr;
- long *dstAddr;
- long thirtyTwoPixels1;
- long thirtyTwoPixels2;
- long thirtyTwoPixels3;
- long thirtyTwoPixels4;
- short mmuMode;
- short row, column;
- short TranslationTable[256];
- unsigned RemapTable[0x211];
- short dstTenBits;
- unsigned char dstChar;
- unsigned long dstLong;
- short width, height;
- short index, index1;
-
- for( index = 0; index < 256; index++ )
- {
- /*
- ** The Translation table takes an index and counts the number of bits set
- **
- ** TranslationTable format: Low 5 bits contain number of bits set in low nibble of index
- ** Next 5 bits contain number of bits set in high nibble
- ** Top 6 bits always zero
- **
- */
- TranslationTable[index] = ((index & 1) != 0) +
- ((index & 2) != 0) +
- ((index & 4) != 0) +
- ((index & 8) != 0) +
- 0x20 * ((index & 0x10) != 0) +
- 0x20 * ((index & 0x20) != 0) +
- 0x20 * ((index & 0x40) != 0) +
- 0x20 * ((index & 0x80) != 0);
- }
- /*
- ** The RemapTable converts a ten bit number into an 8-bit value
- ** The ten bit number is a composite of two five bit numbers which can have
- ** values from 0-16. The result for each 5-bit input is:
- **
- ** 0-7 -> 0-7
- ** 8 -> 7
- ** 9-$10 -> 8-$F
- */
-
- for( index = 0; index <= 0x10; index++ )
- {
- for( index1 = 0; index1 <= 0x10; index1++ )
- RemapTable[(index<<5) + index1] = (char) ((index - index/8 + index/16)<<4) +
- (index1 - index1/8 + index1/16);
- }
-
- srcPixMap = GetGWorldPixMap ( src );
- dstPixMap = GetGWorldPixMap ( dst );
- if ( LockPixels ( srcPixMap ) && LockPixels( dstPixMap) ) { /* lock the pixmaps */
- srcBaseAddr = (long *) GetPixBaseAddr ( srcPixMap ); /* get the address of the pixmap */
- srcRowBytes = (**srcPixMap).rowBytes & 0x7fff; /* get the row increment */
- dstBaseAddr = (long *) GetPixBaseAddr ( dstPixMap ); /* get the address of the pixmap */
- dstRowBytes = (**dstPixMap).rowBytes & 0x7fff; /* get the row increment */
- width = (**srcPixMap).bounds.right-(**srcPixMap).bounds.left;
- height = (**srcPixMap).bounds.bottom-(**srcPixMap).bounds.top;
-
- mmuMode = true32b;
- SwapMMUMode ( &mmuMode ); /* set the MMU to 32-bit mode */
- for ( row = 0; row < (height/4); row++ ) { /* get each pixel in the pixmap */
- srcAddr1 = srcBaseAddr;
- srcAddr2 = (long *) ( (char *) srcAddr1 + srcRowBytes );
- srcAddr3 = (long *) ( (char *) srcAddr2 + srcRowBytes );
- srcAddr4 = (long *) ( (char *) srcAddr3 + srcRowBytes );
- dstAddr = dstBaseAddr;
- for ( column = 0; column < ((width+31)>>5); column++ ) {
- thirtyTwoPixels1 = *srcAddr1++; /* get 4 longs of src */
- thirtyTwoPixels2 = *srcAddr2++; /* get 4 longs of src */
- thirtyTwoPixels3 = *srcAddr3++; /* get 4 longs of src */
- thirtyTwoPixels4 = *srcAddr4++; /* get 4 longs of src */
- dstLong = 0;
- /* Do 8 bits of source */
- dstTenBits = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
- dstChar = RemapTable[dstTenBits];
- dstLong = dstChar;
-
- /* Do 2nd 8 bits of source */
- thirtyTwoPixels1 >>= 8; /* move to 2nd byte */
- thirtyTwoPixels2 >>= 8; /* move to 2nd byte */
- thirtyTwoPixels3 >>= 8; /* move to 2nd byte */
- thirtyTwoPixels4 >>= 8; /* move to 2nd byte */
-
- dstTenBits = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
- dstChar = RemapTable[dstTenBits];
- dstLong += (dstChar<<8);
- /* Do 3rd 8 bits of source */
- thirtyTwoPixels1 >>= 8; /* move to 3nd byte */
- thirtyTwoPixels2 >>= 8;
- thirtyTwoPixels3 >>= 8;
- thirtyTwoPixels4 >>= 8;
-
- dstTenBits = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
- dstChar = RemapTable[dstTenBits];
- dstLong += (long) ((long)dstChar<<16);
- /* Do 4th 8 bits of source */
- thirtyTwoPixels1 >>= 8; /* move to 4th byte */
- thirtyTwoPixels2 >>= 8;
- thirtyTwoPixels3 >>= 8;
- thirtyTwoPixels4 >>= 8;
-
- dstTenBits = TranslationTable[thirtyTwoPixels1 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels2 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels3 & 0x000000FF];
- dstTenBits += TranslationTable[thirtyTwoPixels4 & 0x000000FF];
- dstChar = RemapTable[dstTenBits];
- dstLong += (long)((long)dstChar<<24);
-
- *dstAddr++ = dstLong;
- }
- srcBaseAddr = (long *) ( (char *) srcBaseAddr + 4 * srcRowBytes ); /* go to the next 4 rows */
- dstBaseAddr = (long *) ( (char *) dstBaseAddr + dstRowBytes ); /* go to the next row */
- }
- SwapMMUMode ( &mmuMode ); /* restore the previous MMU mode */
- UnlockPixels ( srcPixMap ); /* unlock the pixmap */
- UnlockPixels ( dstPixMap ); /* unlock the pixmap */
- }
-
- }